package com.example.demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.example.demo.domain.*;
import com.google.gson.Gson;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

public class TestMd5Sign extends BaseMd5{
    static Random r = new Random();
    public static  double getRandomDouble(){
        return  r.nextDouble();
    }


    public static void main(String[] args) {
        TestMd5Sign.getRequestJson();
    }

    /**
     * 加签--复杂json
     */
    public static String getRequestJson(){
        Request request = createRequest();
        String signStr = getSignStr(request);
        request.setSign(signStr);
        Gson g = new Gson();
        String reqJSon = g.toJson(request);
        System.out.println("格式化显示请求报文:\n"+JSON.toJSONString(request,true));
        return reqJSon;
    }

    private static String getSignStr(Request request) {
        String waitSignStr = getWaitingSignStringByFastJson(request);
        System.out.println("待签名串:"+waitSignStr);
        String md5Hex = DigestUtils.md5Hex(waitSignStr);
        System.out.println("md5签名串:"+md5Hex+",转大写:"+md5Hex.toUpperCase());
        return  md5Hex.toUpperCase();
    }

    public static String getWaitingSignStringByFastJson(Request request){
        String key =  BaseMd5.appKey + BaseMd5.appSecret;
        String json =  JSON.toJSONString(request.getData());
        TreeMap<String,Object> treeMap = JSON.parseObject(json,TreeMap.class,Feature.OrderedField);
        StringBuffer sb = new StringBuffer(key);
        sb.append(request.getAppId());
        //将channelType放到公共参数中
        sb.append(request.getChannelType());
        StringBuffer dataWaitSignStr = new StringBuffer();
        for(Map.Entry<String,Object> entry : treeMap.entrySet()){
            dataWaitSignStr.append(entry.getValue());
        }
        sb.append(dataWaitSignStr.toString());
        sb.append(request.getMethod());
        sb.append(request.getTimestamp());
        return sb.toString();
    }


    private static Request createRequest() {
        Request<OilStationInfo> request = new Request<>();
        request.initTimeStamp();
        request.setAppId(BaseMd5.appId);
        request.setChannelType(BaseMd5.channelType);
        request.setMethod("oil.getCardInfo");

        OilStationInfo oil = new OilStationInfo();
        List<GunInfo> gunsList = new ArrayList<>();

        for(int i=0;i<2;i++){
            GunInfo gun = new GunInfo();
            gun.setGunId(i+"");
            gun.setGunName(i+" 号 枪");
            gun.setSkuCode("G09"+(i+4));
            gun.setGunName("G09"+(i+4)+"汽油国VI");
            double guide = getRandomDouble() + 5;
            gun.setGuidePrice(guide);
            gun.setMarketPrice(guide - 1.0);
            gun.setDiscountPrice(guide -  1.1);
            gunsList.add(gun);
        }

        oil.setStationId("12345");
        oil.setStationName("测试油站");
        oil.setProvince("xxxxx");
        oil.setProvinceName("山东省");
        oil.setCity("xxxxxx");
        oil.setCityName("青岛市");
        oil.setDistrict("xxxxxx");
        oil.setDistrictName("李沧区");
        oil.setAddress("团结路8号");
        oil.setTel("022-0859776666");
        oil.setOpentime("09:00-23:00");
        oil.setTagType("A");
        oil.setGuns(JSON.toJSONString(gunsList));
        oil.setPictures("https://xxxxxxx.png");
        oil.setStatus("online");
        oil.setLng("123.0123");
        oil.setLat("123.0223");
        oil.setStationType("nongov");
        oil.setOssCate("2");
        oil.setGasType("G");
        oil.setGasName("汽油");
        oil.setPayType("2");

        request.setData(oil);

        return request;
    }

}
